using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace SBPweb.Common.Configuration
{
	/// <summary>
	/// Class of ConfigurationMetaParser
	/// </summary>
	public class ConfigurationMetaParser
	{
		private string mPattern;
		private List<string> mTags;
		private Dictionary<string, string> mTagValues;
		private bool mIsMatch;

		/// <summary>
		/// List of tags
		/// </summary>
		public List<string> Tags
		{
			get { return mTags; }
			set { mTags = value; }
		}

		/// <summary>
		/// Dictionary of tag values
		/// </summary>
		public Dictionary<string, string> TagValues
		{
			get { return mTagValues; }
			set { mTagValues = value; }
		}

		/// <summary>
		/// Is match?
		/// </summary>
		public bool IsMatch
		{
			get { return mIsMatch; }
			set { mIsMatch = value; }
		}

		/// <summary>
		/// Constructor of ConfigurationMetaParser
		/// </summary>
		/// <param name="pattern">pattern</param>
		public ConfigurationMetaParser(string pattern)
			: this(pattern, string.Empty)
		{
		}

		/// <summary>
		/// Constructor of ConfigurationMetaParser
		/// </summary>
		/// <param name="pattern">pattern</param>
		/// <param name="text">text</param>
		public ConfigurationMetaParser(string pattern, string text)
		{
			mPattern = pattern;
			//Parsing tags
			Regex r = new Regex(@"\[.*\]");
			MatchCollection mc = r.Matches(pattern);
			mTags = new List<string>();
			foreach (Match m in mc)
			{
				if (!mTags.Contains(m.Value))
				{
					mTags.Add(m.Value);
				}
			}

			//Checking if there are duplicate tags
			mIsMatch = (mTags.Count == mc.Count);
			if (mIsMatch)
			{
				//Creating value parser pattern
				r = new Regex(@"\[[^\]]*\]");
				string patt = r.Replace(pattern, "(.*)");
				r = new Regex(patt);
				int groupNumber = r.GetGroupNumbers().Length;

				//Checking pattern
				mIsMatch = r.IsMatch(text);
				if (mIsMatch)
				{
					//Getting actual values
					string evaluator = string.Empty;
					for (int i = 1; i < groupNumber; i++)
					{
						evaluator = evaluator + "<>" + "$" + i.ToString(CultureInfo.CurrentCulture);
					}
					string[] results = r.Replace(text, evaluator).Split(new string[] { "<>" }, StringSplitOptions.RemoveEmptyEntries);

					//Checking the number of tags and values
					mIsMatch = (results.Length == mTags.Count);
					if (mIsMatch)
					{
						//Pairing keys and values
						mTagValues = new Dictionary<string, string>();
						for (int i = 0; i < mc.Count; i++)
						{
							mTagValues.Add(mc[i].Value, results[i]);
						}
					}
				}
			}
			if (!mIsMatch)
			{
				mTagValues = new Dictionary<string, string>();
				foreach (string tag in mTags)
				{
					mTagValues.Add(tag, string.Empty);
				}
			}
		}

		/// <summary>
		/// Get value as string
		/// </summary>
		/// <returns></returns>
		public string GetValueString()
		{
			string result = mPattern;
			foreach (string tag in mTagValues.Keys)
			{
				result = result.Replace(tag, mTagValues[tag]);
			}
			return result;
		}
	}
}
